package com.example.zhinongbao.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.zhinongbao.Model.UserManager;
import com.example.zhinongbao.R;
import com.example.zhinongbao.contract.SettingContact;


public class Setting extends AppCompatActivity implements View.OnClickListener , SettingContact {
    private ImageButton setBackBtn ;
    private LinearLayout setPersonMessage , setLocalManage , setLoginPaw , setPayPaw ;
    private LinearLayout setCallCustomer , setFeedback , setAboutUs , setAboutSoftware ;
    private Button setSignOut ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        setBackBtn = findViewById(R.id.set_back_btn);
        setPersonMessage = findViewById(R.id.setting_person_message) ;
        setLocalManage = findViewById(R.id.setting_local_manager) ;
        setLoginPaw = findViewById(R.id.setting_login_password) ;
        setPayPaw = findViewById(R.id.setting_pay_password) ;
        setCallCustomer = findViewById(R.id.setting_call_customer) ;
        setFeedback = findViewById(R.id.setting_feedback) ;
        setAboutUs = findViewById(R.id.setting_about_us) ;
        setAboutSoftware = findViewById(R.id.setting_about_software);
        setSignOut = findViewById(R.id.setting_sign_out);

        onSetBackBtnClick();
        onSetPersonMsgBtnClick();
        onSetLocalMsgBtnClick();
        onSetLoginPasswordBtnClick();
        onSetPayPasswordBtnClick();
        onSetCallCustomerBtnClick();
        onSetFeedbackBtnClick();
        onSetAboutUsBtnClick();
        onSetAboutSoftwareBtnClick();
        onSetSignOutBtnClick();

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.set_back_btn){
            onBackPressed();
        } else if (v.getId() == R.id.setting_person_message) {
            Intent intent = new Intent(this , PersonalCenter.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_local_manager) {
            Toast.makeText(this, "地址选择功能未开发", Toast.LENGTH_SHORT).show();
        }else if (v.getId() == R.id.setting_login_password){
            Intent intent = new Intent(this , UpdateLoginPassword.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_pay_password) {
            Intent intent = new Intent(this , UpdatePayPassword.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_call_customer) {
            Toast.makeText(this, "电话客服功能未开发", Toast.LENGTH_SHORT).show();
        } else if (v.getId() == R.id.setting_feedback) {
            Intent intent = new Intent(this , FeedbackActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_about_us) {
            Intent intent = new Intent(this , AboutUs.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_about_software) {
            Intent intent = new Intent(this , AboutSoftware.class) ;
            startActivity(intent);
        } else if (v.getId() == R.id.setting_sign_out) {
//            LoginOut();
//
//            Intent intent =new Intent(Setting.this,MainActivity.class);
//            startActivity(intent);
//            Toast.makeText(this, "退出登录成功", Toast.LENGTH_SHORT).show();
            boolean isLogoutSuccessful = LoginOut();

            if (isLogoutSuccessful) {
                // 用户成功退出登录，进行跳转
                Intent intent = new Intent(Setting.this, MainActivity.class);
                startActivity(intent);
                Toast.makeText(this, "退出登录成功", Toast.LENGTH_SHORT).show();
            } else {
                // 用户未登录，提示用户登录
                Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private boolean LoginOut() {
//        if (UserManager.isLoggedIn()) {
//            // 清除用户会话信息
//            UserManager.setLoggedIn(false);
//
//            // 从本地存储中删除用户数据，例如SharedPreferences
//            SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
//            SharedPreferences.Editor editor = sharedPreferences.edit();
//            editor.clear();
//            editor.apply();
        if (UserManager.isLoggedIn()) {
            // 用户已登录，执行退出登录操作
            UserManager.setLoggedIn(false);

            // 从本地存储中删除用户数据
            SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.clear();
            editor.apply();

            // 可以在这里添加其他退出登录需要执行的操作

            // 返回true表示成功退出登录
            return true;
        } else {
            // 用户未登录，返回false
            return false;
        }
            // 可以在这里添加其他需要执行的清理操作，比如注销服务等

            // 通知用户界面更新，显示未登录状态
//            UserInfo emptyUserInfo = new UserInfo(); // 创建一个空的用户信息对象


            // 如果有其他界面需要更新，可以在这里发送广播或使用其他方式通知

    }


    @Override
    public void onSetBackBtnClick() {
        setBackBtn.setOnClickListener(this);
    }

    @Override
    public void onSetPersonMsgBtnClick() {
        setPersonMessage.setOnClickListener(this);
    }

    @Override
    public void onSetLocalMsgBtnClick() {
        setLocalManage.setOnClickListener(this);
    }

    @Override
    public void onSetLoginPasswordBtnClick() {
        setLoginPaw.setOnClickListener(this);
    }

    @Override
    public void onSetPayPasswordBtnClick() {
        setPayPaw.setOnClickListener(this);
    }

    @Override
    public void onSetCallCustomerBtnClick() {
        setCallCustomer.setOnClickListener(this);
    }

    @Override
    public void onSetFeedbackBtnClick() {
        setFeedback.setOnClickListener(this);
    }

    @Override
    public void onSetAboutUsBtnClick() {
        setAboutUs.setOnClickListener(this);
    }

    @Override
    public void onSetAboutSoftwareBtnClick() {
        setAboutSoftware.setOnClickListener(this);
    }

    @Override
    public void onSetSignOutBtnClick() {
        setSignOut.setOnClickListener(this);
    }


    }


